package cn.hzc.fortess.system.controller;

import cn.hzc.fortess.core.common.BizErrorEnum;
import cn.hzc.fortess.core.common.ResponseData;
import cn.hzc.fortess.core.common.annotion.RequestPermission;
import cn.hzc.fortess.core.common.constant.Const;
import cn.hzc.fortess.core.common.exception.ServiceException;
import cn.hzc.fortess.system.common.EnableStatus;
import cn.hzc.fortess.system.entity.Lessee;
import cn.hzc.fortess.system.page.Pagination;
import cn.hzc.fortess.system.search.LesseeSearch;
import cn.hzc.fortess.system.service.LesseeService;
import cn.hzc.fortess.system.service.ProjectService;
import cn.hzc.fortess.system.vo.ProjectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Api(tags = {"租户管理"})
@Controller
@RequestMapping("/sam/lessee")
public class LesseeController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(LesseeController.class);

    @Autowired
    private LesseeService lesseeService;
    @Autowired
    private ProjectService projectService;

    @ApiIgnore
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index() {
        return buildToView("index.html");
    }

    @ApiIgnore
    @RequestMapping("/add")
    public String addToView() {
        Lessee entity = new Lessee();
        entity.setStatus(EnableStatus.ENABLE.getStatus());
        setAttr("lessee", entity);
        setAttr("refprojects", projectService.getUserProject().stream().map(project -> {
            ProjectVO vo = new ProjectVO();
            vo.setProjectId(project.getId());
            vo.setProjectName(project.getProjectName());
            return vo;
        }).collect(Collectors.toList()));
        return buildToView("form.html");
    }

    @ApiOperation(value = "创建租户")
    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData create(Lessee entity) {
        this.lesseeService.add(entity);
        return ResponseData.success();
    }

    @ApiIgnore
    @RequestMapping("/edit/{id}")
    public String editToView(@PathVariable(value = "id", required = true) Integer id) {
        Lessee lessee = lesseeService.getById(id);
        List<Integer> projectIds = lesseeService.getRefProjectByLesseeId(id);
        setAttr("lessee", lessee);
        setAttr("refprojects", projectService.getUserProject().stream().map(project -> {
            ProjectVO vo = new ProjectVO();
            vo.setProjectId(project.getId());
            vo.setProjectName(project.getProjectName());
            if ((!CollectionUtils.isEmpty(projectIds))
                    && (projectIds.contains(project.getId()))) {
                vo.setSelected("selected");
            }
            return vo;
        }).collect(Collectors.toList()));
        return buildToView("form.html");
    }

    @ApiOperation(value = "编辑租户")
    @RequestMapping(value = "/edit.json", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData edit(Lessee entity) {
        if (Objects.equals(Const.SAM_LESSEE_ID, entity.getId())
                && Objects.equals(entity.getStatus(), EnableStatus.DISABLE.getStatus())) {
            throw new ServiceException(BizErrorEnum.CANT_EDIT_STATUS);
        }

        this.lesseeService.update(entity);
        return ResponseData.success();
    }

    @ApiOperation(value = "删除租户")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData delete(@PathVariable(value = "id", required = true) Integer id) {

        if (Objects.equals(Const.SAM_LESSEE_ID, id)) {
            throw new ServiceException(BizErrorEnum.REQUEST_NOT_ALLOW);
        }
        this.lesseeService.delete(id);
        return ResponseData.success();
    }

    @ApiOperation(value = "获取分页列表")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @RequestPermission("sam:lessee:list")
    public Pagination page(LesseeSearch search) {
        return this.lesseeService.getPage(search);
    }

    @Override
    protected String moduleName() {
        return "lessee";
    }
}


